package com.blog;

import com.bean.ScoreAndRank;
import com.linux.huhx.utils.DateFormatUtils;
import com.linux.huhx.utils.SendMailUtils;
import okhttp3.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class GetScoreMain {
    static ScoreAndRank scoreAndRank = new ScoreAndRank();

    public static void main(String[] args) {
        Thread thread = new Thread(new HttpRequestThread());
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(thread, 0, 300, TimeUnit.SECONDS); // 方法在此阻塞
    }
}

// 发送邮件的线程
class SendMailThread implements Runnable {
    private ScoreAndRank scoreAndRank;

    public SendMailThread(ScoreAndRank scoreAndRank) {
        this.scoreAndRank = scoreAndRank;
    }

    @Override
    public void run() {
        String message = "积分 -\t" + scoreAndRank.getScore() + "\n排名 -\t" + scoreAndRank.getRank();
        String string = DateFormatUtils.getNowStringTime() + "：积分 -\t" + scoreAndRank.getScore() + "排名 -\t" + scoreAndRank.getRank();
        System.out.println(string);
        SendMailUtils.sendMail("285490389@qq.com", "博客园的信息", message);
    }
}

// 请求数据的线程
class HttpRequestThread implements Runnable {
    final String url = "http://www.cnblogs.com/huhx/mvc/blog/sidecolumn.aspx?blogApp=huhx";

    @Override
    public void run() {
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String message = response.body().string();
                getScoreInfoFromResponse(message, GetScoreMain.scoreAndRank);
            }

            private void getScoreInfoFromResponse(String message, ScoreAndRank score) {
                Document document = Jsoup.parse(message);
                String scoreString = document.getElementsByClass("liScore").text().split(" - ")[1];
                String rankString = document.getElementsByClass("liRank").text().split(" - ")[1];
                if (rankString.equals(score.getRank()) && scoreString.equals(score.getScore())) {
                    System.out.println(DateFormatUtils.getNowStringTime() + "：没有改变。");
                    return;
                } else {
                    score.setScore(scoreString);
                    score.setRank(rankString);
                    // 开启线程去发送邮件
                    new Thread(new SendMailThread(score)).start();
                }
            }
        });
    }
}
